package vip.fuxk.blog.config;

import cn.zhxu.bs.*;
import cn.zhxu.bs.implement.*;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.solon.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.solon.plugins.inner.PaginationInnerInterceptor;
import com.zaxxer.hikari.HikariDataSource;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.solon.annotation.Db;
import org.noear.solon.annotation.Bean;
import org.noear.solon.annotation.Configuration;
import org.noear.solon.annotation.Inject;
import org.noear.solon.cache.jedis.RedisCacheService;
import org.noear.solon.core.handle.Context;
import org.noear.solon.data.cache.CacheService;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Configuration
@Slf4j
public class DbConfig {
    @Bean(value = "blog_main", typed = true)
    public DataSource blog_main(@Inject("${fuxk.blog_main}") HikariDataSource ds) {
        return ds;
    }

    @Bean(name = "redisCache", typed = true)
    public CacheService redisCache(@Inject("${fuxk.redis}") RedisCacheService cache){
        return cache;
    }



    @Bean
    public void blog_main_cfg(@Db("blog_main") MybatisConfiguration cfg,
                        @Db("blog_main") GlobalConfig globalConfig) {
        MybatisPlusInterceptor plusInterceptor = new MybatisPlusInterceptor();
        plusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));

        cfg.setCacheEnabled(false);
        cfg.addInterceptor(plusInterceptor);

        globalConfig.setSqlInjector(new MyLogicSqlInjector());
    }

    @Bean("currentRequestParamFilter")
    public ParamFilter currentRequestParamFilter() {
        return new BsPramsFilter();
    }

    @Bean
    public PageSizeExtractor pageSizeExtractor() {
        BsPageSizeExtractor p = new BsPageSizeExtractor();
        p.setStart(1);
        p.setDefaultSize(20);
        p.setMaxAllowedSize(300);
        p.setPageName("current");
        p.setSizeName("size");
        return p;
    }



    @Bean
    public  DefaultSqlExecutor defaultSqlExecutor(@Inject("blog_main") DataSource dataSource ){
        DefaultSqlExecutor defaultSqlExecutor = new DefaultSqlExecutor(dataSource);

        return defaultSqlExecutor;
    }

    @Bean
    private DefaultParamResolver defaultParamResolver(
            @Inject("currentRequestParamFilter") ParamFilter paramFilter,
            @Inject PageSizeExtractor pageSizeExtractor
    ){
        DefaultParamResolver resolver = new DefaultParamResolver();
        resolver.setPageExtractor(pageSizeExtractor);
        resolver.addParamFilter(paramFilter);
        return resolver;
    }


    @Bean
    public DefaultBeanSearcher defaultBeanSearcher(@Inject DefaultSqlExecutor executor,
                                                   @Inject DefaultParamResolver   defaultParamResolver){
        DefaultBeanSearcher bs = new DefaultBeanSearcher(executor);
        bs.setParamResolver(defaultParamResolver);
        return bs;
    }


    @Bean
    public DefaultMapSearcher defaultMapSearcher(@Inject DefaultSqlExecutor executor,
                                                 @Inject DefaultParamResolver   defaultParamResolver){
        DefaultMapSearcher bs = new DefaultMapSearcher(executor);
        bs.setParamResolver(defaultParamResolver);
        return bs;
    }

//    @Bean
//    public MybatisSqlSessionFactoryBuilder factoryBuilderNew(){
//        return new MybatisSqlSessionFactoryBuilderImpl();
//    }
}